
import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaTop, WeaRightMenu, WeaNewScroll, WeaAlertPage } from "ecCom"
import { Button, Modal } from "antd"
import { toJS } from "mobx"
import CptInfo from '../public/cptInfo'
import CptModifyLogDialog from '../dialog/CptModifyLogDialog'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptCardStore')
@observer
export default class CptCard1 extends React.Component {
    componentDidMount() {
        const { cptCardStore, location: { query } } = this.props;
        const { getCptInfo, checkRight } = cptCardStore;
        checkRight({ viewtype: 'view', ...query });
    }
    componentWillReceiveProps(nextProps) {
        const keyOld = this.props.location.key;
        const keyNew = nextProps.location.key;
        if (keyOld !== keyNew) {
            const { cptCardStore, location: { query } } = nextProps;
            const { getCptInfo } = cptCardStore;
            getCptInfo({ viewtype: 'view', ...query });
        }
    }
    render() {
        const { cptCardStore } = this.props;
        const { cptForm, cptInfo, loading, setValidate, hasRight, verified } = cptCardStore;
        let titleEle = cptInfo.title;
        if(cptInfo.isCancelled){
            titleEle = (<span style={{color:'red'}}>{cptInfo.title+"("+getLabel(22205,'已封存')+")"}</span>);
        }
        if (verified && !hasRight) {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@svaj6n`} >
                    <div style={{color : '#000'}}>
                        {getLabel(2012,'对不起，您暂时没有权限！')}
                    </div>
                </WeaAlertPage>
            )
        } 
        
        if(verified && hasRight){
            return (
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@67xeys`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}  >
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@focspu`}
                        title={titleEle || ""}
                        loading={loading}
                        icon={<i className='icon-coms-fa' />}
                        iconBgcolor='#f14a2d'
                        buttons={this.getTopButtons()}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                        onDropMenuClick={this.onRightMenuClick.bind(this)}
                    >
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@4ti66t`} height={"100%"}>
                            <CptInfo ecId={`${this && this.props && this.props.ecId || ''}_CptInfo@exqzdw`} form={cptForm} condition={cptInfo} setValidate={setValidate} />
                        </WeaNewScroll>
                        <CptModifyLogDialog ecId={`${this && this.props && this.props.ecId || ''}_CptModifyLogDialog@vbqokv`} store={cptCardStore} />
                    </WeaTop>
                </WeaRightMenu>
            )
        } 

        return (<div></div>)
    }
    getTopButtons() {
        const { cptCardStore } = this.props;
        const { cptInfo, capitalid } = cptCardStore;
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        let btnArr = [];
        let that = this;
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@8bv4ns@${m.type}`} type="primary"
                    onClick={() => {
                        let fn = m.menuFun.indexOf('this') >= 0 ? `${m.menuFun.split('this')[0]})` : m.menuFun;
                        if (m.type == "BTN_EDIT") { //编辑
                            cptCardStore.getCptInfo({ viewtype: 'edit', capitalid: capitalid });
                        } else if (m.type == "BTN_SUBMIT") {  //保存
                            cptCardStore.saveCptInfo({ capitalid: capitalid, isdata: 1 }, "card");
                        }else if (m.type == "BTN_LOG") {  //日志
                            cptCardStore.getModifyLog(capitalid);
                        }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }
    getRightMenu() {
        const { cptCardStore } = this.props;
        const { cptInfo } = cptCardStore;
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName
            })
        });
        return btnArr
    }
    onRightMenuClick(key) {
        const { cptCardStore } = this.props;
        const { cptInfo, capitalid } = cptCardStore;
        let that = this;
        let rightMenu = toJS(cptInfo.rightMenus) || [];
        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            if (Number(key) == i) {
                if (m.type == "BTN_EDIT") { //编辑
                    cptCardStore.getCptInfo({ viewtype: 'edit', capitalid: capitalid });
                } else if (m.type == "BTN_SUBMIT") {  //保存
                    cptCardStore.saveCptInfo({ capitalid: capitalid, isdata: 1 }, "card");
                } else if (m.type == "BTN_DELETE") {  //删除
                    Modal.confirm({
                        title: getLabel(15172, '系统提示'),
                        content: getLabel(83600, '您确认要删除吗?'),
                        onOk() {
                            cptCardStore.onCptDel({ capitalid: capitalid }, 'card', that.updateLeftTree);
                        },
                        onCancel() { },
                    });
                } else if (m.type == "BTN_BACK") { //返回
                    cptCardStore.getCptInfo({ viewtype: 'view', capitalid: capitalid });
                }else if (m.type == "BTN_LOG") {  //日志
                    cptCardStore.getModifyLog(capitalid);
                }
            }
        });
    }

    updateLeftTree = () => {
        const { cptCardStore } = this.props;
        cptCardStore.initTreeDatas();
    }
}
